package com.invest.utils.security;

import org.apache.commons.lang.StringUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

/*
 */
public class PasswordUtils {

	private final static String STATIC_SLAT = "HELLO KITTY";

	private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

	private static final String[] CHARACTORS = { "a", "b", "c", "d", "e", "f",
			"g", "h", "j", "k", "l", "q", "w", "e", "r", "t", "y", "u", "i",
			"o", "p", "z", "x", "c", "v", "b", "n", "m" };

	// 随机生成6为密码(两位字母(第一个大写，第二个小写),4位数字)
	public static String randomPwd() {
		Random rdm = new Random();
		int i = rdm.nextInt(24);
		int j = rdm.nextInt(24);
		int k = rdm.nextInt(10);
		int l = rdm.nextInt(10);
		int m = rdm.nextInt(10);
		int n = rdm.nextInt(10);
		StringBuilder sb = new StringBuilder(CHARACTORS[i].toUpperCase());
		sb.append(CHARACTORS[j]);
		sb.append(k);
		sb.append(l);
		sb.append(m);
		sb.append(n);
		return sb.toString();
	}

	// 对密码进行md5编码
	public static String md5Pwd(String id,String pwd) {
		
		if(StringUtils.isBlank(id) || StringUtils.isBlank(pwd)){
			return null;
		}
		pwd = id.concat(STATIC_SLAT).concat(pwd);
		String md5Pwd = md5Encode(pwd);
		return md5Pwd;
	}
	
	/**
	 * 将字节数组转换为16进制的字符串
	 * 
	 * @param byteArray
	 *            字节数组
	 * @return 16进制的字符串
	 */
	private static  String byteArrayToHexString(byte[] byteArray) {
		StringBuffer sb = new StringBuffer();
		for (byte byt : byteArray) {
			sb.append(byteToHexString(byt));
		}
		return sb.toString();
	}

	/**
	 * 将字节转换为16进制字符串
	 * 
	 * @param byt
	 *            字节
	 * @return 16进制字符串
	 */
	private static  String byteToHexString(byte byt) {
		int n = byt;
		if (n < 0)
			n = 256 + n;
		return hexDigits[n / 16] + hexDigits[n % 16];
	}

	/**
	 * 将摘要信息转换为相应的编码
	 * 
	 * @param code
	 *            编码类型
	 * @param message
	 *            摘要信息
	 * @return 相应的编码字符串
	 */
	private static  String Encode(String code, String message) {
		MessageDigest md;
		String encode = null;
		try {
			md = MessageDigest.getInstance(code);
			encode = byteArrayToHexString(md.digest(message.getBytes()));
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return encode;
	}

	/**
	 * 将摘要信息转换成MD5编码
	 * 
	 * @param message
	 *            摘要信息
	 * @return MD5编码之后的字符串
	 */
	public static String md5Encode(String message) {
		return Encode("MD5", message);
	}

	/**
	 * 将摘要信息转换成SHA编码
	 * 
	 * @param message
	 *            摘要信息
	 * @return SHA编码之后的字符串
	 */
	public String shaEncode(String message) {
		return Encode("SHA", message);
	}

	/**
	 * 将摘要信息转换成SHA-256编码
	 * 
	 * @param message
	 *            摘要信息
	 * @return SHA-256编码之后的字符串
	 */
	public String sha256Encode(String message) {
		return Encode("SHA-256", message);
	}

	/**
	 * 将摘要信息转换成SHA-512编码
	 * 
	 * @param message
	 *            摘要信息
	 * @return SHA-512编码之后的字符串
	 */
	public String sha512Encode(String message) {
		return Encode("SHA-512", message);
	}
	
}
